package IO;

import java.io.*;
import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:进行普通文件的复制
 * 打开文件A, 依次读出每个字节, 再写入到文件B
 * User: 86185
 * Date: 2023-11-29
 * Time: 8:50
 */
public class Demo12 {
    public static void main(String[] args) throws IOException {
        Scanner scanner = new Scanner(System.in);

        System.out.println("请输入要复制的文件的路径");
        String src = scanner.next();
        File srcFile = new File(src);
        if(!srcFile.isFile()) {
            System.out.println("非法文件");
            return;
        }

        System.out.println("请输入复制的目标路径");
        String dst = scanner.next();
        File dstFile = new File(dst);
        //不要求目标文件本身存在, 但是目标文件所在的目录使存在的
        if(!dstFile.getParentFile().isDirectory()) {
            System.out.println("输入的目标文件目录非法");
        }

        try (InputStream inputStream = new FileInputStream(srcFile);
             OutputStream outputStream = new FileOutputStream(dstFile)){
            while (true) {
                byte[] buffer = new byte[1024];
                int n = inputStream.read(buffer);
                if(n == -1) {
                    System.out.println("文件读取完成");
                    break;
                }
                outputStream.write(buffer);
            }
        }
    }
}
